package com.emailsystem.emailsystem.service.impl.client;

import com.emailsystem.emailsystem.domain.Connection;
//import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;

import java.io.ObjectOutputStream;
import java.net.Socket;

@Service
public class oldClientServiceImpl {
    private static Socket socket;
    public static boolean connection_state = false;

    public static Connection connection;

    public static void setState(boolean state) {
        connection_state = state;
    }


    public static boolean getState() {
        return connection_state;
    }


    public static Connection getConnection() {
        return connection;
    }


//    @Override
    public void connect(Connection connection, JSONObject object) {
        try {
            connection = connection;
            socket = new Socket(connection.getIP(), connection.getPort());
            connection_state = true;
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            new Thread(new Client_send(socket, oos, object)).start();
        } catch (Exception e) {
            e.printStackTrace();
            connection_state = false;
        }
    }


    public static void connect(Connection connection, JSONObject object, int num) {
        try {
            socket = new Socket(connection.getIP(), connection.getPort());
            connection_state = true;
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            new Thread(new Client_send(socket, oos, object)).start();
        } catch (Exception e) {
            e.printStackTrace();
            connection_state = false;
        }
    }
}
